package com.research.demo.toolkit;

/**
 * 当连接池启动后会自动定时查询数据库，防止数据库连接超时。
 *
 */
class QueryThread extends Thread {

    private DBHelper dbHelper = null;
    // 是否开启查询
    private boolean startQuery = true;

    /**
     * 功能：对应的数据库连接。
     *
     * @param dbHelper
     *            数据库连接
     */
    QueryThread(DBHelper dbHelper) {
        this.dbHelper = dbHelper;
    }

    public void run() {
        while (true) {
            try {
                if (startQuery) {
                    this.dbHelper.query("select 1");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    Thread.sleep(120000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    void setStartQuery(boolean startQuery) {
        this.startQuery = startQuery;
    }
}


